home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / APPNOTE.ZIP / APPNOTE.TXT next >
Text File  |  1993-01-25  |  37KB  |  1,004 lines

  1.  
  2. Disclaimer
  3. ----------
  4.  
  5. Although PKWARE will attempt to supply current and accurate
  6. information relating to its file formats, algorithms, and the
  7. subject programs, the possibility of error can not be eliminated.
  8. PKWARE therefore expressly disclaims any warranty that the
  9. information contained in the associated materials relating to the
  10. subject programs and/or the format of the files created or
  11. accessed by the subject programs and/or the algorithms used by
  12. the subject programs, or any other matter, is current, correct or
  13. accurate as delivered.  Any risk of damage due to any possible
  14. inaccurate information is assumed by the user of the information.
  15. Furthermore, the information relating to the subject programs
  16. and/or the file formats created or accessed by the subject
  17. programs and/or the algorithms used by the subject programs is
  18. subject to change without notice.
  19.  
  20.  
  21. General Format of a ZIP file
  22. ----------------------------
  23.  
  24.   Files stored in arbitrary order.  Large zipfiles can span multiple
  25.   diskette media.
  26.  
  27.   Overall zipfile format:
  28.  
  29.     [local file header + file data + data_descriptor] . . .
  30.     [central directory] end of central directory record
  31.  
  32.  
  33.   A.  Local file header:
  34.  
  35.     local file header signature     4 bytes  (0x04034b50)
  36.     version needed to extract       2 bytes
  37.     general purpose bit flag        2 bytes
  38.     compression method              2 bytes
  39.     last mod file time              2 bytes
  40.     last mod file date              2 bytes
  41.     crc-32                          4 bytes
  42.     compressed size                 4 bytes
  43.     uncompressed size               4 bytes
  44.     filename length                 2 bytes
  45.     extra field length              2 bytes
  46.  
  47.     filename (variable size)
  48.     extra field (variable size)
  49.  
  50.  
  51.   B.  Data descriptor:
  52.  
  53.     crc-32                          4 bytes
  54.     compressed size                 4 bytes
  55.     uncompressed size               4 bytes
  56.  
  57.       This descriptor exists only if bit 3 of the general
  58.       purpose bit flag is set (see below).  It is byte aligned
  59.       and immediately follows the last byte of compressed data.
  60.       This descriptor is used only when it was not possible to
  61.       seek in the output zip file, e.g., when the output zip file
  62.       was standard output or a non seekable device.
  63.  
  64.   C.  Central directory structure:
  65.  
  66.       [file header] . . .  end of central dir record
  67.  
  68.       File header:
  69.  
  70.     central file header signature   4 bytes  (0x02014b50)
  71.     version made by                 2 bytes
  72.     version needed to extract       2 bytes
  73.     general purpose bit flag        2 bytes
  74.     compression method              2 bytes
  75.     last mod file time              2 bytes
  76.     last mod file date              2 bytes
  77.     crc-32                          4 bytes
  78.     compressed size                 4 bytes
  79.     uncompressed size               4 bytes
  80.     filename length                 2 bytes
  81.     extra field length              2 bytes
  82.     file comment length             2 bytes
  83.     disk number start               2 bytes
  84.     internal file attributes        2 bytes
  85.     external file attributes        4 bytes
  86.     relative offset of local header 4 bytes
  87.  
  88.     filename (variable size)
  89.     extra field (variable size)
  90.     file comment (variable size)
  91.  
  92.       End of central dir record:
  93.  
  94.     end of central dir signature    4 bytes  (0x06054b50)
  95.     number of this disk             2 bytes
  96.     number of the disk with the
  97.     start of the central directory  2 bytes
  98.     total number of entries in
  99.     the central dir on this disk    2 bytes
  100.     total number of entries in
  101.     the central dir                 2 bytes
  102.     size of the central directory   4 bytes
  103.     offset of start of central
  104.     directory with respect to
  105.     the starting disk number        4 bytes
  106.     zipfile comment length          2 bytes
  107.     zipfile comment (variable size)
  108.  
  109.  
  110.   D.  Explanation of fields:
  111.  
  112.       version made by (2 bytes)
  113.  
  114.       The upper byte indicates the host system (OS) for the
  115.       file.  Software can use this information to determine
  116.       the line record format for text files etc.  The current
  117.       mappings are:
  118.  
  119.       0 - MS-DOS and OS/2 (F.A.T. file systems)
  120.       1 - Amiga                     2 - VAX/VMS
  121.       3 - *nix                      4 - VM/CMS
  122.       5 - Atari ST                  6 - OS/2 H.P.F.S.
  123.       7 - Macintosh                 8 - Z-System
  124.       9 - CP/M                      10 thru 255 - unused
  125.  
  126.       The lower byte indicates the version number of the
  127.       software used to encode the file.  The value/10
  128.       indicates the major version number, and the value
  129.       mod 10 is the minor version number.
  130.  
  131.       version needed to extract (2 bytes)
  132.  
  133.       The minimum software version needed to extract the
  134.       file, mapped as above.
  135.  
  136.       general purpose bit flag: (2 bytes)
  137.  
  138.       bit 0: If set, indicates that the file is encrypted.
  139.  
  140.       (For Method 6 - Imploding)
  141.       bit 1: If the compression method used was type 6,
  142.          Imploding, then this bit, if set, indicates
  143.          an 8K sliding dictionary was used.  If clear,
  144.          then a 4K sliding dictionary was used.
  145.       bit 2: If the compression method used was type 6,
  146.          Imploding, then this bit, if set, indicates
  147.          an 3 Shannon-Fano trees were used to encode the
  148.          sliding dictionary output.  If clear, then 2
  149.          Shannon-Fano trees were used.
  150.  
  151.       (For Method 8 - Deflating)
  152.       bit 2  bit 1
  153.         0      0    Normal (-en) compression option was used.
  154.         0      1    Maximum (-ex) compression option was used.
  155.         1      0    Fast (-ef) compression option was used.
  156.         1      1    Super Fast (-es) compression option was used.
  157.  
  158.       Note:  Bits 1 and 2 are undefined if the compression
  159.          method is any other.
  160.  
  161.       (For method 8)
  162.       bit 3: If this bit is set, the fields crc-32, compressed size
  163.          and uncompressed size are set to zero in the local
  164.          header.  The correct values are put in the data descriptor
  165.          immediately following the compressed data.
  166.  
  167.       The upper three bits are reserved and used internally
  168.       by the software when processing the zipfile.  The
  169.       remaining bits are unused.
  170.  
  171.       compression method: (2 bytes)
  172.  
  173.       (see accompanying documentation for algorithm
  174.       descriptions)
  175.  
  176.       0 - The file is stored (no compression)
  177.       1 - The file is Shrunk
  178.       2 - The file is Reduced with compression factor 1
  179.       3 - The file is Reduced with compression factor 2
  180.       4 - The file is Reduced with compression factor 3
  181.       5 - The file is Reduced with compression factor 4
  182.       6 - The file is Imploded
  183.       7 - Reserved for Tokenizing compression algorithm
  184.       8 - The file is Deflated
  185.  
  186.       date and time fields: (2 bytes each)
  187.  
  188.       The date and time are encoded in standard MS-DOS format.
  189.       If input came from standard input, the date and time are
  190.       those at which compression was started for this data.
  191.  
  192.       CRC-32: (4 bytes)
  193.  
  194.       The CRC-32 algorithm was generously contributed by
  195.       David Schwaderer and can be found in his excellent
  196.       book "C Programmers Guide to NetBIOS" published by
  197.       Howard W. Sams & Co. Inc.  The 'magic number' for
  198.       the CRC is 0xdebb20e3.  The proper CRC pre and post
  199.       conditioning is used, meaning that the CRC register
  200.       is pre-conditioned with all ones (a starting value
  201.       of 0xffffffff) and the value is post-conditioned by
  202.       taking the one's complement of the CRC residual.
  203.       If bit 3 of the general purpose flag is set, this
  204.       field is set to zero in the local header and the correct
  205.       value is put in the data descriptor and in the central
  206.       directory.
  207.  
  208.       compressed size: (4 bytes)
  209.       uncompressed size: (4 bytes)
  210.  
  211.       The size of the file compressed and uncompressed,
  212.       respectively.  If bit 3 of the general purpose bit flag
  213.       is set, these fields are set to zero in the local header
  214.       and the correct values are put in the data descriptor and
  215.       in the central directory.
  216.  
  217.       filename length: (2 bytes)
  218.       extra field length: (2 bytes)
  219.       file comment length: (2 bytes)
  220.  
  221.       The length of the filename, extra field, and comment
  222.       fields respectively.  The combined length of any
  223.       directory record and these three fields should not
  224.       generally exceed 65,535 bytes.  If input came from standard
  225.       input, the filename length is set to zero.
  226.  
  227.  
  228.       disk number start: (2 bytes)
  229.  
  230.       The number of the disk on which this file begins.
  231.  
  232.       internal file attributes: (2 bytes)
  233.  
  234.       The lowest bit of this field indicates, if set, that
  235.       the file is apparently an ASCII or text file.  If not
  236.       set, that the file apparently contains binary data.
  237.       The remaining bits are unused in version 1.0.
  238.  
  239.       external file attributes: (4 bytes)
  240.  
  241.       The mapping of the external attributes is
  242.       host-system dependent (see 'version made by').  For
  243.       MS-DOS, the low order byte is the MS-DOS directory
  244.       attribute byte.  If input came from standard input, this
  245.       field is set to zero.
  246.  
  247.       relative offset of local header: (4 bytes)
  248.  
  249.       This is the offset from the start of the first disk on
  250.       which this file appears, to where the local header should
  251.       be found.
  252.  
  253.       filename: (Variable)
  254.  
  255.       The name of the file, with optional relative path.
  256.       The path stored should not contain a drive or
  257.       device letter, or a leading slash.  All slashes
  258.       should be forward slashes '/' as opposed to
  259.       backwards slashes '\' for compatibility with Amiga
  260.       and Unix file systems etc.  If input came from standard
  261.       input, there is no filename field.
  262.  
  263.       extra field: (Variable)
  264.  
  265.       This is for future expansion.  If additional information
  266.       needs to be stored in the future, it should be stored
  267.       here.  Earlier versions of the software can then safely
  268.       skip this file, and find the next file or header.  This
  269.       field will be 0 length in version 1.0.
  270.  
  271.       In order to allow different programs and different types
  272.       of information to be stored in the 'extra' field in .ZIP
  273.       files, the following structure should be used for all
  274.       programs storing data in this field:
  275.  
  276.       header1+data1 + header2+data2 . . .
  277.  
  278.       Each header should consist of:
  279.  
  280.         Header ID - 2 bytes
  281.         Data Size - 2 bytes
  282.  
  283.       Note: all fields stored in Intel low-byte/high-byte order.
  284.  
  285.       The Header ID field indicates the type of data that is in
  286.       the following data block.
  287.  
  288.       Header ID's of 0 thru 31 are reserved for use by PKWARE.
  289.       The remaining ID's can be used by third party vendors for
  290.       proprietary usage.
  291.  
  292.       The current Header ID mappings are:
  293.  
  294.       0x0007        AV Info
  295.       0x0009        OS/2
  296.       0x000c        VAX/VMS
  297.  
  298.       The Data Size field indicates the size of the following
  299.       data block. Programs can use this value to skip to the
  300.       next header block, passing over any data blocks that are
  301.       not of interest.
  302.  
  303.       Note: As stated above, the size of the entire .ZIP file
  304.         header, including the filename, comment, and extra
  305.         field should not exceed 64K in size.
  306.  
  307.       In case two different programs should appropriate the same
  308.       Header ID value, it is strongly recommended that each
  309.       program place a unique signature of at least two bytes in
  310.       size (and preferably 4 bytes or bigger) at the start of
  311.       each data area.  Every program should verify that its
  312.       unique signature is present, in addition to the Header ID
  313.       value being correct, before assuming that it is a block of
  314.       known type.
  315.  
  316.      -VAX/VMS Extra Field:
  317.  
  318.       The following is the layout of the VAX/VMS attributes "extra"
  319.       block.  (Last Revision 12/17/91)
  320.  
  321.       Note: all fields stored in Intel low-byte/high-byte order.
  322.  
  323.       Value         Size            Description
  324.       -----         ----            -----------
  325.   (VMS)   0x000c        Short           Tag for this "extra" block type
  326.       TSize         Short           Size of the total "extra" block
  327.       CRC           Long            32-bit CRC for remainder of the block
  328.       Tag1          Short           VMS attribute tag value #1
  329.       Size1         Short           Size of attribute #1, in bytes
  330.       (var.)        Size1           Attribute #1 data
  331.       .
  332.       .
  333.       .
  334.       TagN          Short           VMS attribute tage value #N
  335.       SizeN         Short           Size of attribute #N, in bytes
  336.       (var.)        SizeN           Attribute #N data
  337.  
  338.       Rules:
  339.  
  340.       1. There will be one or more of attributes present, which will
  341.          each be preceded by the above TagX & SizeX values.  These
  342.          values are identical to the ATR$C_XXXX and ATR$S_XXXX constants
  343.          which are defined in ATR.H under VMS C.  Neither of these values
  344.          will ever be zero.
  345.  
  346.       2. No word alignment or padding is performed.
  347.  
  348.       3. A well-behaved PKZIP/VMS program should never produce more than
  349.          one sub-block with the same TagX value.  Also, there will never
  350.          be more than one "extra" block of type 0x000c in a particular
  351.          directory record.
  352.  
  353.       file comment: (Variable)
  354.  
  355.       The comment for this file.
  356.  
  357.       number of this disk: (2 bytes)
  358.  
  359.       The number of this disk, which contains central
  360.       directory end record.
  361.  
  362.       number of the disk with the start of the central directory: (2 bytes)
  363.  
  364.       The number of the disk on which the central
  365.       directory starts.
  366.  
  367.       total number of entries in the central dir on this disk: (2 bytes)
  368.  
  369.       The number of central directory entries on this disk.
  370.  
  371.       total number of entries in the central dir: (2 bytes)
  372.  
  373.       The total number of files in the zipfile.
  374.  
  375.  
  376.       size of the central directory: (4 bytes)
  377.  
  378.       The size (in bytes) of the entire central directory.
  379.  
  380.       offset of start of central directory with respect to
  381.       the starting disk number:  (4 bytes)
  382.  
  383.       Offset of the start of the central direcory on the
  384.       disk on which the central directory starts.
  385.  
  386.       zipfile comment length: (2 bytes)
  387.  
  388.       The length of the comment for this zipfile.
  389.  
  390.       zipfile comment: (Variable)
  391.  
  392.       The comment for this zipfile.
  393.  
  394.  
  395.   D.  General notes:
  396.  
  397.       1)  All fields unless otherwise noted are unsigned and stored
  398.       in Intel low-byte:high-byte, low-word:high-word order.
  399.  
  400.       2)  String fields are not null terminated, since the
  401.       length is given explicitly.
  402.  
  403.       3)  Local headers should not span disk boundries.  Also, even
  404.       though the central directory can span disk boundries, no
  405.       single record in the central directory should be split
  406.       across disks.
  407.  
  408.       4)  The entries in the central directory may not necessarily
  409.       be in the same order that files appear in the zipfile.
  410.  
  411. UnShrinking - Method 1
  412. ----------------------
  413.  
  414. Shrinking is a Dynamic Ziv-Lempel-Welch compression algorithm
  415. with partial clearing.  The initial code size is 9 bits, and
  416. the maximum code size is 13 bits.  Shrinking differs from
  417. conventional Dynamic Ziv-Lempel-Welch implementations in several
  418. respects:
  419.  
  420. 1)  The code size is controlled by the compressor, and is not
  421.     automatically increased when codes larger than the current
  422.     code size are created (but not necessarily used).  When
  423.     the decompressor encounters the code sequence 256
  424.     (decimal) followed by 1, it should increase the code size
  425.     read from the input stream to the next bit size.  No
  426.     blocking of the codes is performed, so the next code at
  427.     the increased size should be read from the input stream
  428.     immediately after where the previous code at the smaller
  429.     bit size was read.  Again, the decompressor should not
  430.     increase the code size used until the sequence 256,1 is
  431.     encountered.
  432.  
  433. 2)  When the table becomes full, total clearing is not
  434.     performed.  Rather, when the compresser emits the code
  435.     sequence 256,2 (decimal), the decompressor should clear
  436.     all leaf nodes from the Ziv-Lempel tree, and continue to
  437.     use the current code size.  The nodes that are cleared
  438.     from the Ziv-Lempel tree are then re-used, with the lowest
  439.     code value re-used first, and the highest code value
  440.     re-used last.  The compressor can emit the sequence 256,2
  441.     at any time.
  442.  
  443.  
  444.  
  445. Expanding - Methods 2-5
  446. -----------------------
  447.  
  448. The Reducing algorithm is actually a combination of two
  449. distinct algorithms.  The first algorithm compresses repeated
  450. byte sequences, and the second algorithm takes the compressed
  451. stream from the first algorithm and applies a probabilistic
  452. compression method.
  453.  
  454. The probabilistic compression stores an array of 'follower
  455. sets' S(j), for j=0 to 255, corresponding to each possible
  456. ASCII character.  Each set contains between 0 and 32
  457. characters, to be denoted as S(j)[0],...,S(j)[m], where m<32.
  458. The sets are stored at the beginning of the data area for a
  459. Reduced file, in reverse order, with S(255) first, and S(0)
  460. last.
  461.  
  462. The sets are encoded as { N(j), S(j)[0],...,S(j)[N(j)-1] },
  463. where N(j) is the size of set S(j).  N(j) can be 0, in which
  464. case the follower set for S(j) is empty.  Each N(j) value is
  465. encoded in 6 bits, followed by N(j) eight bit character values
  466. corresponding to S(j)[0] to S(j)[N(j)-1] respectively.  If
  467. N(j) is 0, then no values for S(j) are stored, and the value
  468. for N(j-1) immediately follows.
  469.  
  470. Immediately after the follower sets, is the compressed data
  471. stream.  The compressed data stream can be interpreted for the
  472. probabilistic decompression as follows:
  473.  
  474.  
  475. let Last-Character <- 0.
  476. loop until done
  477.     if the follower set S(Last-Character) is empty then
  478.     read 8 bits from the input stream, and copy this
  479.     value to the output stream.
  480.     otherwise if the follower set S(Last-Character) is non-empty then
  481.     read 1 bit from the input stream.
  482.     if this bit is not zero then
  483.         read 8 bits from the input stream, and copy this
  484.         value to the output stream.
  485.     otherwise if this bit is zero then
  486.         read B(N(Last-Character)) bits from the input
  487.         stream, and assign this value to I.
  488.         Copy the value of S(Last-Character)[I] to the
  489.         output stream.
  490.  
  491.     assign the last value placed on the output stream to
  492.     Last-Character.
  493. end loop
  494.  
  495.  
  496. B(N(j)) is defined as the minimal number of bits required to
  497. encode the value N(j)-1.
  498.  
  499.  
  500. The decompressed stream from above can then be expanded to
  501. re-create the original file as follows:
  502.  
  503.  
  504. let State <- 0.
  505.  
  506. loop until done
  507.     read 8 bits from the input stream into C.
  508.     case State of
  509.     0:  if C is not equal to DLE (144 decimal) then
  510.         copy C to the output stream.
  511.         otherwise if C is equal to DLE then
  512.         let State <- 1.
  513.  
  514.     1:  if C is non-zero then
  515.         let V <- C.
  516.         let Len <- L(V)
  517.         let State <- F(Len).
  518.         otherwise if C is zero then
  519.         copy the value 144 (decimal) to the output stream.
  520.         let State <- 0
  521.  
  522.     2:  let Len <- Len + C
  523.         let State <- 3.
  524.  
  525.     3:  move backwards D(V,C) bytes in the output stream
  526.         (if this position is before the start of the output
  527.         stream, then assume that all the data before the
  528.         start of the output stream is filled with zeros).
  529.         copy Len+3 bytes from this position to the output stream.
  530.         let State <- 0.
  531.     end case
  532. end loop
  533.  
  534.  
  535. The functions F,L, and D are dependent on the 'compression
  536. factor', 1 through 4, and are defined as follows:
  537.  
  538. For compression factor 1:
  539.     L(X) equals the lower 7 bits of X.
  540.     F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  541.     D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  542. For compression factor 2:
  543.     L(X) equals the lower 6 bits of X.
  544.     F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  545.     D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  546. For compression factor 3:
  547.     L(X) equals the lower 5 bits of X.
  548.     F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  549.     D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  550. For compression factor 4:
  551.     L(X) equals the lower 4 bits of X.
  552.     F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  553.     D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  554.  
  555.  
  556. Imploding - Method 6
  557. --------------------
  558.  
  559. The Imploding algorithm is actually a combination of two distinct
  560. algorithms.  The first algorithm compresses repeated byte
  561. sequences using a sliding dictionary.  The second algorithm is
  562. used to compress the encoding of the sliding dictionary ouput,
  563. using multiple Shannon-Fano trees.
  564.  
  565. The Imploding algorithm can use a 4K or 8K sliding dictionary
  566. size. The dictionary size used can be determined by bit 1 in the
  567. general purpose flag word; a 0 bit indicates a 4K dictionary
  568. while a 1 bit indicates an 8K dictionary.
  569.  
  570. The Shannon-Fano trees are stored at the start of the compressed
  571. file. The number of trees stored is defined by bit 2 in the
  572. general purpose flag word; a 0 bit indicates two trees stored, a
  573. 1 bit indicates three trees are stored.  If 3 trees are stored,
  574. the first Shannon-Fano tree represents the encoding of the
  575. Literal characters, the second tree represents the encoding of
  576. the Length information, the third represents the encoding of the
  577. Distance information.  When 2 Shannon-Fano trees are stored, the
  578. Length tree is stored first, followed by the Distance tree.
  579.  
  580. The Literal Shannon-Fano tree, if present is used to represent
  581. the entire ASCII character set, and contains 256 values.  This
  582. tree is used to compress any data not compressed by the sliding
  583. dictionary algorithm.  When this tree is present, the Minimum
  584. Match Length for the sliding dictionary is 3.  If this tree is
  585. not present, the Minimum Match Length is 2.
  586.  
  587. The Length Shannon-Fano tree is used to compress the Length part
  588. of the (length,distance) pairs from the sliding dictionary
  589. output.  The Length tree contains 64 values, ranging from the
  590. Minimum Match Length, to 63 plus the Minimum Match Length.
  591.  
  592. The Distance Shannon-Fano tree is used to compress the Distance
  593. part of the (length,distance) pairs from the sliding dictionary
  594. output. The Distance tree contains 64 values, ranging from 0 to
  595. 63, representing the upper 6 bits of the distance value.  The
  596. distance values themselves will be between 0 and the sliding
  597. dictionary size, either 4K or 8K.
  598.  
  599. The Shannon-Fano trees themselves are stored in a compressed
  600. format. The first byte of the tree data represents the number of
  601. bytes of data representing the (compressed) Shannon-Fano tree
  602. minus 1.  The remaining bytes represent the Shannon-Fano tree
  603. data encoded as:
  604.  
  605.     High 4 bits: Number of values at this bit length + 1. (1 - 16)
  606.     Low  4 bits: Bit Length needed to represent value + 1. (1 - 16)
  607.  
  608. The Shannon-Fano codes can be constructed from the bit lengths
  609. using the following algorithm:
  610.  
  611. 1)  Sort the Bit Lengths in ascending order, while retaining the
  612.     order of the original lengths stored in the file.
  613.  
  614. 2)  Generate the Shannon-Fano trees:
  615.  
  616.     Code <- 0
  617.     CodeIncrement <- 0
  618.     LastBitLength <- 0
  619.     i <- number of Shannon-Fano codes - 1   (either 255 or 63)
  620.  
  621.     loop while i >= 0
  622.     Code = Code + CodeIncrement
  623.     if BitLength(i) <> LastBitLength then
  624.         LastBitLength=BitLength(i)
  625.         CodeIncrement = 1 shifted left (16 - LastBitLength)
  626.     ShannonCode(i) = Code
  627.     i <- i - 1
  628.     end loop
  629.  
  630.  
  631. 3)  Reverse the order of all the bits in the above ShannonCode()
  632.     vector, so that the most significant bit becomes the least
  633.     significant bit.  For example, the value 0x1234 (hex) would
  634.     become 0x2C48 (hex).
  635.  
  636. 4)  Restore the order of Shannon-Fano codes as originally stored
  637.     within the file.
  638.  
  639. Example:
  640.  
  641.     This example will show the encoding of a Shannon-Fano tree
  642.     of size 8.  Notice that the actual Shannon-Fano trees used
  643.     for Imploding are either 64 or 256 entries in size.
  644.  
  645. Example:   0x02, 0x42, 0x01, 0x13
  646.  
  647.     The first byte indicates 3 values in this table.  Decoding the
  648.     bytes:
  649.         0x42 = 5 codes of 3 bits long
  650.         0x01 = 1 code  of 2 bits long
  651.         0x13 = 2 codes of 4 bits long
  652.  
  653.     This would generate the original bit length array of:
  654.     (3, 3, 3, 3, 3, 2, 4, 4)
  655.  
  656.     There are 8 codes in this table for the values 0 thru 7.  Using the
  657.     algorithm to obtain the Shannon-Fano codes produces:
  658.  
  659.                   Reversed     Order     Original
  660. Val  Sorted   Constructed Code      Value     Restored    Length
  661. ---  ------   -----------------   --------    --------    ------
  662. 0:     2      1100000000000000        11       101          3
  663. 1:     3      1010000000000000       101       001          3
  664. 2:     3      1000000000000000       001       110          3
  665. 3:     3      0110000000000000       110       010          3
  666. 4:     3      0100000000000000       010       100          3
  667. 5:     3      0010000000000000       100        11          2
  668. 6:     4      0001000000000000      1000      1000          4
  669. 7:     4      0000000000000000      0000      0000          4
  670.  
  671.  
  672. The values in the Val, Order Restored and Original Length columns
  673. now represent the Shannon-Fano encoding tree that can be used for
  674. decoding the Shannon-Fano encoded data.  How to parse the
  675. variable length Shannon-Fano values from the data stream is beyond the
  676. scope of this document.  (See the references listed at the end of
  677. this document for more information.)  However, traditional decoding
  678. schemes used for Huffman variable length decoding, such as the
  679. Greenlaw algorithm, can be succesfully applied.
  680.  
  681. The compressed data stream begins immediately after the
  682. compressed Shannon-Fano data.  The compressed data stream can be
  683. interpreted as follows:
  684.  
  685. loop until done
  686.     read 1 bit from input stream.
  687.  
  688.     if this bit is non-zero then       (encoded data is literal data)
  689.     if Literal Shannon-Fano tree is present
  690.         read and decode character using Literal Shannon-Fano tree.
  691.     otherwise
  692.         read 8 bits from input stream.
  693.     copy character to the output stream.
  694.     otherwise                   (encoded data is sliding dictionary match)
  695.     if 8K dictionary size
  696.         read 7 bits for offset Distance (lower 7 bits of offset).
  697.     otherwise
  698.         read 6 bits for offset Distance (lower 6 bits of offset).
  699.  
  700.     using the Distance Shannon-Fano tree, read and decode the
  701.       upper 6 bits of the Distance value.
  702.  
  703.     using the Length Shannon-Fano tree, read and decode
  704.       the Length value.
  705.  
  706.     Length <- Length + Minimum Match Length
  707.  
  708.     if Length = 63 + Minimum Match Length
  709.         read 8 bits from the input stream,
  710.         add this value to Length.
  711.  
  712.     move backwards Distance+1 bytes in the output stream, and
  713.     copy Length characters from this position to the output
  714.     stream.  (if this position is before the start of the output
  715.     stream, then assume that all the data before the start of
  716.     the output stream is filled with zeros).
  717. end loop
  718.  
  719. Tokenizing - Method 7
  720. --------------------
  721.  
  722. This method is not used by PKZIP.
  723.  
  724. Deflating - Method 8
  725. -----------------
  726.  
  727. The Deflate algorithm is similar to the Implode algorithm using
  728. a sliding dictionary of up to 32K with secondary compression
  729. from Huffman/Shannon-Fano codes.
  730.  
  731. The compressed data is stored in blocks with a header describing
  732. the block and the Huffman codes used in the data block.  The header
  733. format is as follows:
  734.  
  735.    Bit 0: Last Block bit     This bit is set to 1 if this is the last
  736.                  compressed block in the data.
  737.    Bits 1-2: Block type
  738.       00 (0) - Block is stored - All stored data is byte aligned.
  739.            Skip bits until next byte, then next word = block length,
  740.            followed by the ones compliment of the block length word.
  741.            Remaining data in block is the stored data.
  742.  
  743.       01 (1) - Use fixed Huffman codes for literal and distance codes.
  744.            Lit Code    Bits             Dist Code   Bits
  745.            ---------   ----             ---------   ----
  746.          0 - 143    8                 0 - 31      5
  747.            144 - 255    9
  748.            256 - 279    7
  749.            280 - 287    8
  750.  
  751.            Literal codes 286-287 and distance codes 30-31 are never
  752.            used but participate in the huffman construction.
  753.  
  754.       10 (2) - Dynamic Huffman codes.  (See expanding Huffman codes)
  755.  
  756.       11 (3) - Reserved - Flag a "Error in compressed data" if seen.
  757.  
  758. Expanding Huffman Codes
  759. -----------------------
  760. If the data block is stored with dynamic Huffman codes, the Huffman
  761. codes are sent in the following compressed format:
  762.  
  763.    5 Bits: # of Literal codes sent - 256 (256 - 286)
  764.        All other codes are never sent.
  765.    5 Bits: # of Dist codes - 1           (1 - 32)
  766.    4 Bits: # of Bit Length codes - 3     (3 - 19)
  767.  
  768. The Huffman codes are sent as bit lengths and the codes are built as
  769. described in the implode algorithm.  The bit lengths themselves are
  770. compressed with Huffman codes.  There are 19 bit length codes:
  771.  
  772.    0 - 15: Represent bit lengths of 0 - 15
  773.        16: Copy the previous bit length 3 - 6 times.
  774.        The next 2 bits indicate repeat length (0 = 3, ... ,3 = 6)
  775.           Example:  Codes 8, 16 (+2 bits 11), 16 (+2 bits 10) will
  776.             expand to 12 bit lengths of 8 (1 + 6 + 5)
  777.        17: Repeat a bit length of 0 for 3 - 10 times. (3 bits of length)
  778.        18: Repeat a bit length of 0 for 11 - 138 times (7 bits of length)
  779.  
  780. The lengths of the bit length codes are sent packed 3 bits per value
  781. (0 - 7) in the following order:
  782.  
  783.    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  784.  
  785. The Huffman codes should be built as described in the Implode algorithm
  786. except codes are assigned starting at the shortest bit length, i.e. the
  787. shortest code should be all 0's rather than all 1's.  Also, codes with
  788. a bit length of zero do not participate in the tree construction.  The
  789. codes are then used to decode the bit lengths for the literal and distance
  790. tables.
  791.  
  792. The bit lengths for the literal tables are sent first with the number
  793. of entries sent described by the 5 bits sent earlier.  There are up
  794. to 286 literal characters; the first 256 represent the respective 8
  795. bit character, code 256 represents the End-Of-Block code, the remaining
  796. 29 codes represent copy lengths of 3 thru 258.  There are up to 30
  797. distance codes representing distances from 1 thru 32k as described
  798. below.
  799.  
  800.                  Length Codes
  801.                  ------------
  802.       Extra             Extra              Extra              Extra
  803.  Code Bits Length  Code Bits Lengths  Code Bits Lengths  Code Bits Length(s)
  804.  ---- ---- ------  ---- ---- -------  ---- ---- -------  ---- ---- ---------
  805.   257   0     3     265   1   11,12    273   3   35-42    281   5  131-162
  806.   258   0     4     266   1   13,14    274   3   43-50    282   5  163-194
  807.   259   0     5     267   1   15,16    275   3   51-58    283   5  195-226
  808.   260   0     6     268   1   17,18    276   3   59-66    284   5  227-257
  809.   261   0     7     269   2   19-22    277   4   67-82    285   0    258
  810.   262   0     8     270   2   23-26    278   4   83-98
  811.   263   0     9     271   2   27-30    279   4   99-114
  812.   264   0    10     272   2   31-34    280   4  115-130
  813.  
  814.                 Distance Codes
  815.                 --------------
  816.       Extra           Extra             Extra               Extra
  817.  Code Bits Dist  Code Bits  Dist   Code Bits Distance  Code Bits Distance
  818.  ---- ---- ----  ---- ---- ------  ---- ---- --------  ---- ---- --------
  819.    0   0    1      8   3   17-24    16    7  257-384    24   11  4097-6144
  820.    1   0    2      9   3   25-32    17    7  385-512    25   11  6145-8192
  821.    2   0    3     10   4   33-48    18    8  513-768    26   12  8193-12288
  822.    3   0    4     11   4   49-64    19    8  769-1024   27   12 12289-16384
  823.    4   1   5,6    12   5   65-96    20    9 1025-1536   28   13 16385-24576
  824.    5   1   7,8    13   5   97-128   21    9 1537-2048   29   13 24577-32768
  825.    6   2   9-12   14   6  129-192   22   10 2049-3072
  826.    7   2  13-16   15   6  193-256   23   10 3073-4096
  827.  
  828. The compressed data stream begins immediately after the
  829. compressed header data.  The compressed data stream can be
  830. interpreted as follows:
  831.  
  832. do
  833.    read header from input stream.
  834.  
  835.    if stored block
  836.       skip bits until byte aligned
  837.       read count and 1's compliment of count
  838.       copy count bytes data block
  839.    otherwise
  840.       loop until end of block code sent
  841.      decode literal character from input stream
  842.      if literal < 256
  843.         copy character to the output stream
  844.      otherwise
  845.         if literal = end of block
  846.            break from loop
  847.         otherwise
  848.            decode distance from input stream
  849.  
  850.            move backwards distance bytes in the output stream, and
  851.            copy length characters from this position to the output
  852.            stream.
  853.       end loop
  854. while not last block
  855.  
  856. if data descriptor exists
  857.    skip bits until byte aligned
  858.    read crc and sizes
  859. endif
  860.  
  861. Decryption
  862. ----------
  863.  
  864. The encryption used in PKZIP was generously supplied by Roger
  865. Schlafly.  PKWARE is grateful to Mr. Schlafly for his expert
  866. help and advice in the field of data encryption.
  867.  
  868. PKZIP encrypts the compressed data stream.  Encrypted files must
  869. be decrypted before they can be extracted.
  870.  
  871. Each encrypted file has an extra 12 bytes stored at the start of
  872. the data area defining the encryption header for that file.  The
  873. encryption header is originally set to random values, and then
  874. itself encrypted, using three, 32-bit keys.  The key values are
  875. initialized using the supplied encryption password.  After each byte
  876. is encrypted, the keys are then updated using pseudo-random number
  877. generation techniques in combination with the same CRC-32 algorithm
  878. used in PKZIP and described elsewhere in this document.
  879.  
  880. The following is the basic steps required to decrypt a file:
  881.  
  882. 1) Initialize the three 32-bit keys with the password.
  883. 2) Read and decrypt the 12-byte encryption header, further
  884.    initializing the encryption keys.
  885. 3) Read and decrypt the compressed data stream using the
  886.    encryption keys.
  887.  
  888.  
  889. Step 1 - Initializing the encryption keys
  890. -----------------------------------------
  891.  
  892. Key(0) <- 305419896
  893. Key(1) <- 591751049
  894. Key(2) <- 878082192
  895.  
  896. loop for i <- 0 to length(password)-1
  897.     update_keys(password(i))
  898. end loop
  899.  
  900.  
  901. Where update_keys() is defined as:
  902.  
  903.  
  904. update_keys(char):
  905.   Key(0) <- crc32(key(0),char)
  906.   Key(1) <- Key(1) + (Key(0) & 000000ffH)
  907.   Key(1) <- Key(1) * 134775813 + 1
  908.   Key(2) <- crc32(key(2),key(1) >> 24)
  909. end update_keys
  910.  
  911.  
  912. Where crc32(old_crc,char) is a routine that given a CRC value and a
  913. character, returns an updated CRC value after applying the CRC-32
  914. algorithm described elsewhere in this document.
  915.  
  916.  
  917. Step 2 - Decrypting the encryption header
  918. -----------------------------------------
  919.  
  920. The purpose of this step is to further initialize the encryption
  921. keys, based on random data, to render a plaintext attack on the
  922. data ineffective.
  923.  
  924.  
  925. Read the 12-byte encryption header into Buffer, in locations
  926. Buffer(0) thru Buffer(11).
  927.  
  928. loop for i <- 0 to 11
  929.     C <- buffer(i) ^ decrypt_byte()
  930.     update_keys(C)
  931.     buffer(i) <- C
  932. end loop
  933.  
  934.  
  935. Where decrypt_byte() is defined as:
  936.  
  937.  
  938. unsigned char decrypt_byte()
  939.     local unsigned short temp
  940.     temp <- Key(2) | 2
  941.     decrypt_byte <- (temp * (temp ^ 1)) >> 8
  942. end decrypt_byte
  943.  
  944.  
  945. After the header is decrypted,  the last 1 or 2 bytes in Buffer
  946. should be the high-order word/byte of the CRC for the file being
  947. decrypted, stored in Intel low-byte/high-byte order.  Versions of
  948. PKZIP prior to 2.0 used a 2 byte CRC check; a 1 byte CRC check is
  949. used on versions after 2.0.  This can be used to test if the password
  950. supplied is correct or not.
  951.  
  952.  
  953. Step 3 - Decrypting the compressed data stream
  954. ----------------------------------------------
  955.  
  956. The compressed data stream can be decrypted as follows:
  957.  
  958.  
  959. loop until done
  960.     read a charcter into C
  961.     Temp <- C ^ decrypt_byte()
  962.     update_keys(temp)
  963.     output Temp
  964. end loop
  965.  
  966.  
  967. In addition to the above mentioned contributors to PKZIP and PKUNZIP,
  968. I would like to extend special thanks to Robert Mahoney for suggesting
  969. the extension .ZIP for this software.
  970.  
  971.  
  972. References:
  973.  
  974.     Fiala, Edward R., and Greene, Daniel H., "Data compression with
  975.        finite windows",  Communications of the ACM, Volume 32, Number 4,
  976.        April 1989, pages 490-505.
  977.  
  978.     Held, Gilbert, "Data Compression, Techniques and Applications,
  979.             Hardware and Software Considerations",
  980.        John Wiley & Sons, 1987.
  981.  
  982.     Huffman, D.A., "A method for the construction of minimum-redundancy
  983.        codes", Proceedings of the IRE, Volume 40, Number 9, September 1952,
  984.        pages 1098-1101.
  985.  
  986.     Nelson, Mark, "LZW Data Compression", Dr. Dobbs Journal, Volume 14,
  987.        Number 10, October 1989, pages 29-37.
  988.  
  989.     Nelson, Mark, "The Data Compression Book",  M&T Books, 1991.
  990.  
  991.     Storer, James A., "Data Compression, Methods and Theory",
  992.        Computer Science Press, 1988
  993.  
  994.     Welch, Terry, "A Technique for High-Performance Data Compression",
  995.        IEEE Computer, Volume 17, Number 6, June 1984, pages 8-19.
  996.  
  997.     Ziv, J. and Lempel, A., "A universal algorithm for sequential data
  998.        compression", Communications of the ACM, Volume 30, Number 6,
  999.        June 1987, pages 520-540.
  1000.  
  1001.     Ziv, J. and Lempel, A., "Compression of individual sequences via
  1002.        variable-rate coding", IEEE Transactions on Information Theory,
  1003.        Volume 24, Number 5, September 1978, pages 530-536.
  1004.